<?php

class MultiFormValidator extends AbstractValidator {

    private $BoIsValidForm = true;

    public function addRule($StName, Rule $Rule) {
        foreach( $this->ArFields as $StIndex => &$MxField) {
            if( !$this->isField($MxField) ) {
                if( parent::fieldExists($StIndex, $StName) ) {
                    $MxField[$StName]->addRule($Rule);
                }
                continue;
            }
            parent::addRule($StName, $Rule);
        }
    }

    public function addRules($StName, Array $Rules) {
        foreach( $this->ArFields as $StIndex => &$MxField) {
            if( !$this->isField($MxField) ) {
                if( parent::fieldExists($StIndex, $StName) ) {
                    $MxField[$StName]->addRules($Rules);
                }
                continue;
            }
            parent::addRules($StName, $Rules);
        }
    }

    public function validate() {
        foreach( $this->ArFields as $MxField ) {
            if( !$this->isField($MxField) ) {
                $this->validateMultipleFields($MxField);
                continue;
            }
            $this->validateSingleField($MxField);
        }
        return $this->BoIsValidForm;
    }

    private function validateMultipleFields($MxField) {
        foreach( $MxField as $Field ) {
            $this->validateSingleField($Field);
        }
    }

    private function validateSingleField($Field) {
        if( $Field->isNotValid($Field) ) {
            $this->ArErrors = array_merge( $this->ArErrors, $Field->getFieldErrors() );
            $this->BoIsValidForm = false;
        }
    }

    protected function isField( $MxPossibleField ) {
        return !is_array($MxPossibleField);
    }

}

?>
